/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itttq.gshop.domain.po.SysFloorPcPo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.FloorPcTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.generic.util.Constants.WareStatus;
import com.itttq.gshop.pbpm.mapper.ISysFloorPcMapper;
import com.itttq.gshop.pbpm.mapper.IWareInfoMapper;

@Service
public class SysFloorPcService {
	@Autowired
	private ISysFloorPcMapper floorMapper;
	@Autowired
	private IWareInfoMapper wareMapper;
	
	/**
	 * 根据ID获取SysFloorPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return SysFloorPcPo
	 */
	public SysFloorPcPo getSysFloor(int id) {
		return floorMapper.findSysFloorById(id);
	}
	
	/**
	 * 根据条件获取SysFloorPo对象，并返回查询的数据总数
	 * 
	 * @param floor
	 * @Author 迷蝶
	 * @return ResultPageTo<SysFloorPcPo>
	 */
	public ResultPageTo<SysFloorPcPo> getSysFloorsByPage(SysFloorPcPo floor) {
		return new ResultPageTo<SysFloorPcPo>(floor.getStart(), floor.getSize(), 
				floorMapper.findSysFloorCount(floor), floorMapper.findSysFloors(floor));
	}
	
	/**
	 * 根据条件获取SysFloorPo对象，不分页
	 * 
	 * @param floor
	 * @Author 迷蝶
	 * @return ResultPageTo<SysFloorPcPo>
	 */
	public List<SysFloorPcPo> getSysFloors(SysFloorPcPo floor) {
		return floorMapper.findSysFloors(floor);
	}
	
	/**
	 * 根据ID删除SysFloorPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 */
	public Integer delSysFloor(int id) {
		return floorMapper.delSysFloorById(id);
	}
	
	/**
	 * 增加SysFloorPo对象
	 * 
	 * @param floor
	 * @Author 迷蝶
	 */
	public Integer addSysFloor(SysFloorPcPo floor) {
		return floorMapper.addSysFloor(floor);
	}
	
	/**
	 * 根据条件更新SysFloorPo对象
	 * 
	 * @param floor
	 * @Author 迷蝶
	 */
	public Integer updateSysFloor(SysFloorPcPo floor) {
		return floorMapper.updateSysFloor(floor);
	}
	
	/**
	 * 获取首页所有楼层以及楼层分类数据<br>
	 * 如有栏目楼层，则在楼层表增加一个分类来做
	 * 
	 * @Author 迷蝶
	 * @return List<FLoorTo>
	 */
	public List<FloorPcTo> getFloorsAndDatas() {
		SysFloorPcPo floor = new SysFloorPcPo();
		floor.setStatus(Constants.STATUTS_OPEN);
		floor.setSortMethod("order"); // 会在mapper时替换
		floor.setSize(9999);
		List<SysFloorPcPo> floors = floorMapper.findSysFloors(floor);
		if(null == floors || floors.isEmpty()) {
			return null;
		}
		// 设置到orderTo的集合中
		List<FloorPcTo> floorPcTos = new ArrayList<FloorPcTo>();
		WareInfoPo wareInfo = new WareInfoPo();
		wareInfo.setStatus(WareStatus.ONSHELF.getVal());
		wareInfo.setRecommend(Constants.STATUTS_OPEN);// 取推荐商品
		for(SysFloorPcPo sfp : floors) {
			wareInfo.setCategoryId(sfp.getCategoryId());
			// 判断是否配置了楼层大图，如果配置，则只获取7调商品数据，反之获取10条
			wareInfo.setSize(StringUtils.isBlank(sfp.getBigImage()) ? 10 : 7);
			FloorPcTo ft = new FloorPcTo();
			BeanUtils.copyProperties(sfp, ft);
			ft.setWares(wareMapper.findWareInfos(wareInfo));
			floorPcTos.add(ft);
		}
		return floorPcTos;
	}
}
